/**
 * 角色管理 - 编辑页面
 * @author yuan_rz
 * @date 2019/01/04 14:21
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            detail: false,
            form: '',
            rules: {
                NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                TYPE: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                IS_ENABLE: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions:{
                sys_role_is_enable : app.datas_('sys_role_is_enable'),
                // sys_role_type : app.datas_('sys_role_type')
                sys_role_type : app.datas_('role')
            }
        },
        callObj,
        pageVue
    ;

    var roleEdit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'role_edit'
            }, win);
        },
        editRole: function () {
            app.ajax({
                xurl: 'sys/role/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '角色信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        callObj.update(result.xdata);
                        layer.close(layer.index);
                        //self._closeTpl();
                    }
                }
            });
        },
        addRole: function () {
            app.ajax({
                xurl: 'sys/role/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '角色信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('role').reloadTableCurr();
                        layer.close(layer.index);
                        //self._closeTpl();
                    }
                }
            });
        },
        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            vueData.detail = false;
            if (obj.is_add) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        TYPE: '',
                        IS_ENABLE: '1'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = obj.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                if(obj.detail){
                    vueData.detail = true;
                }else{
                    vueData.detail = false;
                }
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['sys_role']);
            pageVue = new Vue({
                el: '#role_edit',
                data: vueData,
                methods: {
                    openRoleWindow: function () {
                        app.log('openRoleWindow');
                    },
                    cancel: function () {
                        if(vueData.detail) {
                            layer.close(layer.index);
                        }else{
                            self._closeTpl();
                        }
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addRole();
                                } else {
                                    self.editRole();
                                }
                            }
                        });
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('role_edit');
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });

    return new roleEdit();

});